// cart 模块

export default {
  // 当使用了 modules 选项拆分之后，一定要配置一个命名空间
  namespaced: true,

  state: {
    // 购物车数据
    cartList: [
      { id: 1, name: "apple", price: 100 },
      { id: 2, name: "banana", price: 200 },
    ],
  },

  getters: {
    length(state) {
      // state 是当前模块中定义的state
      return state.cartList.length;
    },

    total(state) {
      return state.cartList.reduce((total, item) => (total += item.price), 0);
    },
  },

  mutations: {
    addGood(state) {
      state.cartList.push({ id: 3, name: "orange", price: 300 });
    },
  },

  actions: {
    syncAddGood({ commit }, payload) {
      setTimeout(() => {
        commit("addGood");
      }, 1000);
    },
  },
};
